package com.xjscrm.server.service.msg.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.mobile.collect.api.dto.MicaTriggerDto;
import com.uzai.mobile.collect.api.dto.trigger.MicaTriggerConversationTalkMsg;
import com.uzai.mobile.collect.api.dto.trigger.group.MicaTriggerGroupAddMsg;
import com.uzai.mobile.collect.api.dto.trigger.group.MicaTriggerGroupInviteMsg;
import com.uzai.mobile.collect.api.enums.MicaTalkMsgType;
import com.uzai.mobile.collect.api.enums.MicaTriggerType;
import com.xjscrm.common.annotation.MerId;
import com.xjscrm.common.entity.GroupPullMarketing;
import com.xjscrm.common.entity.WwCustomer;
import com.xjscrm.common.utils.DateUtil;
import com.xjscrm.common.utils.Tools;
import com.xjscrm.common.vo.grouppull.AgainData;
import com.xjscrm.common.vo.grouppull.GroupPullData;
import com.xjscrm.common.vo.grouppull.GroupPullMsgData;
import com.xjscrm.common.vo.msg.MsgData;
import com.xjscrm.server.biz.mq.producer.SendMsgProducer;
import com.xjscrm.server.common.util.Functions;
import com.xjscrm.server.common.util.MsgDataTools;
import com.xjscrm.server.mapper.GroupPullMarketingMapper;
import com.xjscrm.server.service.msg.MsgSendService;
import com.xjscrm.server.utils.DelayTopicCustomerAddHolder;
import com.xjscrm.server.utils.DelayTopicHolder;
import com.xjscrm.server.vo.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业微信机器人(WwDevice)表服务实现类
 *
 * @author makejava
 * @since 2024-03-01 17:01:12
 */
@Service
@Slf4j
public class MsgSendServiceImpl implements MsgSendService {
    @Autowired
    private SendMsgProducer sendMsgProducer;
    @Autowired
    private GroupPullMarketingMapper groupPullMarketingMapper;

    /**
     * 发送消息接口
     *
     * @param device 机器人信息
     * @return 实例对象
     */
    @Async("taskExecutorForMsg")
    public void msgSend(Device device, String wxid, String nickName, List<MsgData> msgDatas, boolean atAll, List<String> atWxIds, int msgsource, Boolean group) {

        if (device == null) {
            return;
        }

        if (StringUtils.isBlank(wxid)) {
            return;
        }

        if (msgDatas == null || msgDatas.size() == 0) {
            return;
        }

        int delay = 0; //默认延迟时间为0;
        for (int i = 0; i < msgDatas.size(); i++) {
            MsgData msgData = msgDatas.get(i);
            delay = delay + Tools.getInteger(msgData.getGap()) * Tools.getInteger(msgData.getUnit());
            MicaTriggerConversationTalkMsg micaTriggerConversationTalkMsg = new MicaTriggerConversationTalkMsg();
            micaTriggerConversationTalkMsg.setContentType(msgData.getMsgType().intValue()); //类型
            micaTriggerConversationTalkMsg.setWxId(wxid); //发送方
            micaTriggerConversationTalkMsg.setGroup(group);
            micaTriggerConversationTalkMsg.setSource(msgsource);

            //处理文本消息替换变量
            if (msgData.getMsgType().intValue() == MicaTalkMsgType.TEXT.getCode().intValue()) {//文本消息
                String content = Tools.getStr(msgData.getContent());
                content = content.replace("[称呼/昵称]", Tools.getStr(nickName));
                msgData.setContent(content);
            }
            //转换成移动端接收的content格式
            String talkMsgContent = MsgDataTools.getTalkMsgContent(msgData);
            //赋值消息内容
            micaTriggerConversationTalkMsg.setContent(talkMsgContent);
            micaTriggerConversationTalkMsg.setAtAll(atAll);
            micaTriggerConversationTalkMsg.setAtWxIds(atWxIds);
            MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerConversationTalkMsg, MicaTriggerType.TRIGGER_CONVERSATION_TALK, Functions.fillTriggerMsg(device));
            micaTriggerDto.setDelay(delay);
            sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息
        }
    }

    /**
     * 发送消息接口
     *
     * @param device 机器人信息
     * @return 实例对象
     */
    @Async("taskExecutorForMsg")
    public void msgSend(Device device, String wxid, String nickName, MsgData msgData, boolean atAll, List<String> atWxIds, int difTime, int msgsource, Boolean group) {
        if (device == null) {
            return;
        }
        if (StringUtils.isBlank(wxid)) {
            return;
        }
        if (msgData == null) {
            return;
        }
        int delay = 0;
        delay = delay + Tools.getInteger(msgData.getGap()) * Tools.getInteger(msgData.getUnit());
        MicaTriggerConversationTalkMsg micaTriggerConversationTalkMsg = new MicaTriggerConversationTalkMsg();
        micaTriggerConversationTalkMsg.setContentType(msgData.getMsgType().intValue()); //类型
        micaTriggerConversationTalkMsg.setWxId(wxid); //发送方
        micaTriggerConversationTalkMsg.setSource(msgsource);
        micaTriggerConversationTalkMsg.setGroup(group);

        //处理文本消息替换变量
        if (msgData.getMsgType().intValue() == MicaTalkMsgType.TEXT.getCode().intValue()) {//文本消息
            String content = Tools.getStr(msgData.getContent());
            content = content.replace("[称呼/昵称]", Tools.getStr(nickName));
            msgData.setContent(content);
        }
        //转换成移动端接收的content格式
        String talkMsgContent = MsgDataTools.getTalkMsgContent(msgData);

        //赋值消息内容
        micaTriggerConversationTalkMsg.setContent(talkMsgContent);
        micaTriggerConversationTalkMsg.setAtAll(atAll);
        micaTriggerConversationTalkMsg.setAtWxIds(atWxIds);
        MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerConversationTalkMsg, MicaTriggerType.TRIGGER_CONVERSATION_TALK, Functions.fillTriggerMsg(device));
        micaTriggerDto.setDelay(delay - difTime < 0 ? 0 : delay - difTime);
        sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息
    }

    /**
     * 发送拉群消息接口
     *
     * @param device 机器人信息
     * @return 实例对象
     */
    @Async("taskExecutorForMsg")
    public void groupPullMsgSend(@MerId Long merId, Device device, WwCustomer wwCustomer, List<GroupPullMsgData> groupPullMsgDataList, Integer type, Long planId, boolean atAll, List<String> atWxIds, int msgsource, Boolean group) {
        if (device == null) {
            return;
        }

        if (wwCustomer == null) {
            return;
        }

        String wxid = wwCustomer.getWxid();

        if (StringUtils.isBlank(wxid)) {
            return;
        }

        if (groupPullMsgDataList == null || groupPullMsgDataList.size() == 0) {
            return;
        }

        int delay = 0; //默认延迟时间为0;
        for (int i = 0; i < groupPullMsgDataList.size(); i++) {
            GroupPullMsgData groupPullMsgData = groupPullMsgDataList.get(i);
            delay = delay + Tools.getInteger(groupPullMsgData.getGap()) * Tools.getInteger(groupPullMsgData.getUnit());
            Integer msgType = Tools.getInteger(groupPullMsgData.getMsgType());
            if (msgType.intValue() != 101 && msgType.intValue() != 102) {//普通消息
                MicaTriggerConversationTalkMsg micaTriggerConversationTalkMsg = new MicaTriggerConversationTalkMsg();
                micaTriggerConversationTalkMsg.setContentType(groupPullMsgData.getMsgType().intValue()); //类型
                micaTriggerConversationTalkMsg.setWxId(wxid); //发送方
                micaTriggerConversationTalkMsg.setSource(msgsource);
                micaTriggerConversationTalkMsg.setGroup(group);

                //处理文本消息替换变量
                if (groupPullMsgData.getMsgType().intValue() == MicaTalkMsgType.TEXT.getCode().intValue()) {//文本消息
                    String content = Tools.getStr(groupPullMsgData.getContent());
                    //替换[称呼/昵称]变量，优先称呼，在昵称
                    String nickName = wwCustomer.getNickname();
                    if (StringUtils.isBlank(nickName)) {
                        nickName = wwCustomer.getName();
                    }
                    content = content.replace("[称呼/昵称]", Tools.getStr(nickName));
                    groupPullMsgData.setContent(content);
                }

                //转换成移动端接收的content格式
                String talkMsgContent = MsgDataTools.getTalkMsgContent(groupPullMsgData);

                //赋值消息内容
                micaTriggerConversationTalkMsg.setContent(talkMsgContent);
                micaTriggerConversationTalkMsg.setAtAll(atAll);
                micaTriggerConversationTalkMsg.setAtWxIds(atWxIds);
                MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerConversationTalkMsg, MicaTriggerType.TRIGGER_CONVERSATION_TALK, Functions.fillTriggerMsg(device));
                micaTriggerDto.setDelay(delay);
                sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息
            } else {
                if (msgType.intValue() == 101) { //拉进旧群
                    //发送拉群指令
                    MicaTriggerGroupInviteMsg micaTriggerGroupInviteMsg = new MicaTriggerGroupInviteMsg();
                    micaTriggerGroupInviteMsg.setGroupId(groupPullMsgData.getGroupId());
                    micaTriggerGroupInviteMsg.setWxIds(groupPullMsgData.getWxIds());
                    MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerGroupInviteMsg, MicaTriggerType.TRIGGER_GROUP_INVITE, Functions.fillTriggerMsg(device));
                    micaTriggerDto.setDelay(delay);
                    sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息

                    try {
                        //未进群营销数据
                        List<AgainData> notJoinInvitationAgain = groupPullMsgData.getNotJoinInvitationAgain();
                        if (notJoinInvitationAgain != null && notJoinInvitationAgain.size() > 0) {//插入数据库
                            for (AgainData againData : notJoinInvitationAgain) {
                                //未进群，再次邀请间隔时间(单位：秒）
                                Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                                //未进群，再次邀请拉群数据列表
                                List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                                if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                    //拉近旧群才有未进群再次拉群营销数据，因为拉新群直接就进群了
                                    GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                    groupPullMarketing.setId(IdWorker.getId());
                                    groupPullMarketing.setMerId(wwCustomer.getMerId());
                                    groupPullMarketing.setType(type);
                                    groupPullMarketing.setPlanId(planId);
                                    groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                    groupPullMarketing.setDeviceId(device.getDeviceId());
                                    groupPullMarketing.setWxid(wwCustomer.getWxid());
                                    groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                    groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                    groupPullMarketing.setStartTime(DateUtil.getTime());
                                    groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                    groupPullMarketing.setStatus(1); //开启中，拉进旧群默认不开启
                                    groupPullMarketing.setModel(0); //不进群营销
                                    groupPullMarketing.setHander(0);
                                    groupPullMarketing.setHanderTime(null);
                                    groupPullMarketing.setCreateTime(DateUtil.getTime());
                                    groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                    groupPullMarketingMapper.insertSelective(groupPullMarketing);

                                    log.info("--监控系统日志--查询未进群营销数据--");

                                }
                            }
                        }

                        //开启已进群再次营销功能
                        List<AgainData> joinedMarketingAgain = groupPullMsgData.getJoinedMarketingAgain();
                        if (joinedMarketingAgain != null && joinedMarketingAgain.size() > 0) {//插入数据库
                            for (AgainData againData : joinedMarketingAgain) {
                                //已进群，再次营销间隔时间(单位：秒）
                                Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                                //已进群，再次营销拉群数据列表
                                List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                                if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                    GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                    groupPullMarketing.setId(IdWorker.getId());
                                    groupPullMarketing.setMerId(wwCustomer.getMerId());
                                    groupPullMarketing.setType(type);
                                    groupPullMarketing.setPlanId(planId);
                                    groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                    groupPullMarketing.setDeviceId(device.getDeviceId());
                                    groupPullMarketing.setWxid(wwCustomer.getWxid());
                                    groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                    groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                    groupPullMarketing.setStartTime(DateUtil.getTime());
                                    groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                    groupPullMarketing.setStatus(0);  //关闭中，进群通过才开启
                                    groupPullMarketing.setModel(1); //已进群营销
                                    groupPullMarketing.setHander(0);
                                    groupPullMarketing.setHanderTime(null);
                                    groupPullMarketing.setCreateTime(DateUtil.getTime());
                                    groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                    groupPullMarketingMapper.insertSelective(groupPullMarketing);

                                    log.info("--监控系统日志--开启已进群再次营销功能--");

                                }
                            }
                        }
                    } catch (Exception e) {
                        log.error("插入入群营销数据失败", e.getMessage(), e);
                    }
                } else if (msgType.intValue() == 102) { //创建新群
                    //发送创建群指令
                    MicaTriggerGroupAddMsg micaTriggerGroupAddMsg = new MicaTriggerGroupAddMsg();
                    micaTriggerGroupAddMsg.setGroupName(groupPullMsgData.getGroupName());
                    micaTriggerGroupAddMsg.setWxIds(groupPullMsgData.getWxIds());
                    MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerGroupAddMsg, MicaTriggerType.TRIGGER_GROUP_ADD, Functions.fillTriggerMsg(device));
                    micaTriggerDto.setDelay(delay);
                    sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息

                    try {
                        //开启已进群再次营销功能
                        List<AgainData> joinedMarketingAgain = groupPullMsgData.getJoinedMarketingAgain();
                        if (joinedMarketingAgain != null && joinedMarketingAgain.size() > 0) {//插入数据库
                            for (AgainData againData : joinedMarketingAgain) {
                                //已进群，再次营销间隔时间(单位：秒）
                                Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                                //已进群，再次营销拉群数据列表
                                List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                                if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                    GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                    groupPullMarketing.setId(IdWorker.getId());
                                    groupPullMarketing.setMerId(wwCustomer.getMerId());
                                    groupPullMarketing.setType(type);
                                    groupPullMarketing.setPlanId(planId);
                                    groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                    groupPullMarketing.setDeviceId(device.getDeviceId());
                                    groupPullMarketing.setWxid(wwCustomer.getWxid());
                                    groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                    groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                    groupPullMarketing.setStartTime(DateUtil.getTime());
                                    groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                    groupPullMarketing.setStatus(1); //开启中，创建新群表示直接拉进去了
                                    groupPullMarketing.setModel(1);  //已进群营销
                                    groupPullMarketing.setHander(0);
                                    groupPullMarketing.setHanderTime(null);
                                    groupPullMarketing.setCreateTime(DateUtil.getTime());
                                    groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                    groupPullMarketingMapper.insertSelective(groupPullMarketing);

                                    log.info("--监控系统日志--开启已进群再次营销功能--");
                                }
                            }
                        }
                    } catch (Exception e) {
                        log.error("插入拉群营销数据失败，error={}", e.getMessage(), e);
                    }
                }
            }
        }
    }

    /**
     * 发送新客户运营消息接口
     *
     * @param device 机器人信息
     * @return 实例对象
     */
    @Async("taskExecutorForNewCustomer")
    public void groupPullMsgSendForCustomerAdd(@MerId Long merId, Device device, WwCustomer wwCustomer, List<GroupPullMsgData> groupPullMsgDataList, Integer type, Long planId, boolean atAll, List<String> atWxIds, int msgsource, Boolean group) {
        if (device == null) {
            return;
        }

        if (wwCustomer == null) {
            return;
        }

        String wxid = wwCustomer.getWxid();

        if (StringUtils.isBlank(wxid)) {
            return;
        }

        if (groupPullMsgDataList == null || groupPullMsgDataList.size() == 0) {
            return;
        }

        int delay = 0; //默认延迟时间为0;
        for (int i = 0; i < groupPullMsgDataList.size(); i++) {
            GroupPullMsgData groupPullMsgData = groupPullMsgDataList.get(i);
            delay = delay + Tools.getInteger(groupPullMsgData.getGap()) * Tools.getInteger(groupPullMsgData.getUnit());
            Integer msgType = Tools.getInteger(groupPullMsgData.getMsgType());
            if (msgType.intValue() != 101 && msgType.intValue() != 102) {//普通消息
                MicaTriggerConversationTalkMsg micaTriggerConversationTalkMsg = new MicaTriggerConversationTalkMsg();
                micaTriggerConversationTalkMsg.setContentType(groupPullMsgData.getMsgType().intValue()); //类型
                micaTriggerConversationTalkMsg.setWxId(wxid); //发送方

                //处理文本消息替换变量
                if (groupPullMsgData.getMsgType().intValue() == MicaTalkMsgType.TEXT.getCode().intValue()) {//文本消息
                    String content = Tools.getStr(groupPullMsgData.getContent());
                    //替换[称呼/昵称]变量，优先称呼，在昵称
                    String nickName = wwCustomer.getNickname();
                    if (StringUtils.isBlank(nickName)) {
                        nickName = wwCustomer.getName();
                    }
                    content = content.replace("[称呼/昵称]", Tools.getStr(nickName));
                    groupPullMsgData.setContent(content);
                }

                //转换成移动端接收的content格式
                String talkMsgContent = MsgDataTools.getTalkMsgContent(groupPullMsgData);

                //赋值消息内容
                micaTriggerConversationTalkMsg.setContent(talkMsgContent);
                micaTriggerConversationTalkMsg.setAtAll(atAll);
                micaTriggerConversationTalkMsg.setAtWxIds(atWxIds);
                micaTriggerConversationTalkMsg.setSource(msgsource);
                micaTriggerConversationTalkMsg.setGroup(group);

                MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerConversationTalkMsg, MicaTriggerType.TRIGGER_CONVERSATION_TALK, Functions.fillTriggerMsg(device));
                micaTriggerDto.setDelay(delay);
                sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicCustomerAddHolder.getNextServer()); //发送新客户运营延迟消息
            } else {
                if (msgType.intValue() == 101) { //拉进旧群
                    //发送拉群指令
                    MicaTriggerGroupInviteMsg micaTriggerGroupInviteMsg = new MicaTriggerGroupInviteMsg();
                    micaTriggerGroupInviteMsg.setGroupId(groupPullMsgData.getGroupId());
                    micaTriggerGroupInviteMsg.setWxIds(groupPullMsgData.getWxIds());
                    MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerGroupInviteMsg, MicaTriggerType.TRIGGER_GROUP_INVITE, Functions.fillTriggerMsg(device));
                    micaTriggerDto.setDelay(delay);
                    sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicCustomerAddHolder.getNextServer()); //发送延迟消息

                    try {
                        //未进群营销数据
                        List<AgainData> notJoinInvitationAgain = groupPullMsgData.getNotJoinInvitationAgain();
                        if (notJoinInvitationAgain != null && notJoinInvitationAgain.size() > 0) {//插入数据库
                            for (AgainData againData : notJoinInvitationAgain) {
                                //未进群，再次邀请间隔时间(单位：秒）
                                Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                                //未进群，再次邀请拉群数据列表
                                List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                                if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                    //拉近旧群才有未进群再次拉群营销数据，因为拉新群直接就进群了
                                    GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                    groupPullMarketing.setId(IdWorker.getId());
                                    groupPullMarketing.setMerId(wwCustomer.getMerId());
                                    groupPullMarketing.setType(type);
                                    groupPullMarketing.setPlanId(planId);
                                    groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                    groupPullMarketing.setDeviceId(device.getDeviceId());
                                    groupPullMarketing.setWxid(wwCustomer.getWxid());
                                    groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                    groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                    groupPullMarketing.setStartTime(DateUtil.getTime());
                                    groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                    groupPullMarketing.setStatus(1); //开启中，拉进旧群默认不开启
                                    groupPullMarketing.setModel(0); //不进群营销
                                    groupPullMarketing.setHander(0);
                                    groupPullMarketing.setHanderTime(null);
                                    groupPullMarketing.setCreateTime(DateUtil.getTime());
                                    groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                    groupPullMarketingMapper.insertSelective(groupPullMarketing);

                                    log.info("--监控系统日志--查询未进群营销数据--");

                                }
                            }
                        }

                        //开启已进群再次营销功能
                        List<AgainData> joinedMarketingAgain = groupPullMsgData.getJoinedMarketingAgain();
                        if (joinedMarketingAgain != null && joinedMarketingAgain.size() > 0) {//插入数据库
                            for (AgainData againData : joinedMarketingAgain) {
                                //已进群，再次营销间隔时间(单位：秒）
                                Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                                //已进群，再次营销拉群数据列表
                                List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                                if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                    GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                    groupPullMarketing.setId(IdWorker.getId());
                                    groupPullMarketing.setMerId(wwCustomer.getMerId());
                                    groupPullMarketing.setType(type);
                                    groupPullMarketing.setPlanId(planId);
                                    groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                    groupPullMarketing.setDeviceId(device.getDeviceId());
                                    groupPullMarketing.setWxid(wwCustomer.getWxid());
                                    groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                    groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                    groupPullMarketing.setStartTime(DateUtil.getTime());
                                    groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                    groupPullMarketing.setStatus(0);  //关闭中，进群通过才开启
                                    groupPullMarketing.setModel(1); //已进群营销
                                    groupPullMarketing.setHander(0);
                                    groupPullMarketing.setHanderTime(null);
                                    groupPullMarketing.setCreateTime(DateUtil.getTime());
                                    groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                    groupPullMarketingMapper.insertSelective(groupPullMarketing);

                                    log.info("--监控系统日志--开启已进群再次营销功能--");

                                }
                            }
                        }
                    } catch (Exception e) {
                        log.error("插入入群营销数据失败", e.getMessage(), e);
                    }
                } else if (msgType.intValue() == 102) { //创建新群
                    //发送创建群指令
                    MicaTriggerGroupAddMsg micaTriggerGroupAddMsg = new MicaTriggerGroupAddMsg();
                    micaTriggerGroupAddMsg.setGroupName(groupPullMsgData.getGroupName());
                    micaTriggerGroupAddMsg.setWxIds(groupPullMsgData.getWxIds());
                    MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerGroupAddMsg, MicaTriggerType.TRIGGER_GROUP_ADD, Functions.fillTriggerMsg(device));
                    micaTriggerDto.setDelay(delay);
                    sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicCustomerAddHolder.getNextServer()); //发送延迟消息

                    try {
                        //开启已进群再次营销功能
                        List<AgainData> joinedMarketingAgain = groupPullMsgData.getJoinedMarketingAgain();
                        if (joinedMarketingAgain != null && joinedMarketingAgain.size() > 0) {//插入数据库
                            for (AgainData againData : joinedMarketingAgain) {
                                //已进群，再次营销间隔时间(单位：秒）
                                Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                                //已进群，再次营销拉群数据列表
                                List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                                if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                    GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                    groupPullMarketing.setId(IdWorker.getId());
                                    groupPullMarketing.setMerId(wwCustomer.getMerId());
                                    groupPullMarketing.setType(type);
                                    groupPullMarketing.setPlanId(planId);
                                    groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                    groupPullMarketing.setDeviceId(device.getDeviceId());
                                    groupPullMarketing.setWxid(wwCustomer.getWxid());
                                    groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                    groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                    groupPullMarketing.setStartTime(DateUtil.getTime());
                                    groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                    groupPullMarketing.setStatus(1); //开启中，创建新群表示直接拉进去了
                                    groupPullMarketing.setModel(1);  //已进群营销
                                    groupPullMarketing.setHander(0);
                                    groupPullMarketing.setHanderTime(null);
                                    groupPullMarketing.setCreateTime(DateUtil.getTime());
                                    groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                    groupPullMarketingMapper.insertSelective(groupPullMarketing);

                                    log.info("--监控系统日志--开启已进群再次营销功能--");
                                }
                            }
                        }
                    } catch (Exception e) {
                        log.error("插入拉群营销数据失败，error={}", e.getMessage(), e);
                    }
                }
            }
        }
    }

    /**
     * 发送拉群消息接口
     *
     * @param device 机器人信息
     * @return 实例对象
     */
    @Async("taskExecutorForMsg")
    public void groupPullMsgSend(@MerId Long merId, Device device, String wxid, String nickName, GroupPullMsgData groupPullMsgData, Integer type, Long planId, boolean atAll, List<String> atWxIds, int difTime, int msgsource, Boolean group) {
        if (device == null) {
            return;
        }

        if (groupPullMsgData == null) {
            return;
        }

        int delay = 0; //默认延迟时间为0;
        delay = delay + Tools.getInteger(groupPullMsgData.getGap()) * Tools.getInteger(groupPullMsgData.getUnit());
        Integer msgType = Tools.getInteger(groupPullMsgData.getMsgType());
        if (msgType.intValue() != 101 && msgType.intValue() != 102) {
            MicaTriggerConversationTalkMsg micaTriggerConversationTalkMsg = new MicaTriggerConversationTalkMsg();
            micaTriggerConversationTalkMsg.setContentType(groupPullMsgData.getMsgType().intValue()); //类型
            micaTriggerConversationTalkMsg.setWxId(wxid); //发送方
            micaTriggerConversationTalkMsg.setSource(msgsource);
            micaTriggerConversationTalkMsg.setGroup(group);
            //处理文本消息替换变量
            if (groupPullMsgData.getMsgType().intValue() == MicaTalkMsgType.TEXT.getCode().intValue()) {//文本消息
                String content = Tools.getStr(groupPullMsgData.getContent());
                //替换[称呼/昵称]变量，优先称呼，在昵称
                content = content.replace("[称呼/昵称]", Tools.getStr(nickName));
                groupPullMsgData.setContent(content);
            }

            //转换成移动端接收的content格式
            String talkMsgContent = MsgDataTools.getTalkMsgContent(groupPullMsgData);

            //赋值消息内容
            micaTriggerConversationTalkMsg.setContent(talkMsgContent);
            micaTriggerConversationTalkMsg.setAtAll(atAll);
            micaTriggerConversationTalkMsg.setAtWxIds(atWxIds);
            MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerConversationTalkMsg, MicaTriggerType.TRIGGER_CONVERSATION_TALK, Functions.fillTriggerMsg(device));
            micaTriggerDto.setDelay(delay - difTime < 0 ? 0 : delay - difTime);
            sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息
        } else {
            if (msgType.intValue() == 101) { //拉进旧群
                //发送拉群指令
                MicaTriggerGroupInviteMsg micaTriggerGroupInviteMsg = new MicaTriggerGroupInviteMsg();
                micaTriggerGroupInviteMsg.setGroupId(groupPullMsgData.getGroupId());
                micaTriggerGroupInviteMsg.setWxIds(groupPullMsgData.getWxIds());
                MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerGroupInviteMsg, MicaTriggerType.TRIGGER_GROUP_INVITE, Functions.fillTriggerMsg(device));
                micaTriggerDto.setDelay(delay);
                sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息

                try {
                    //未进群营销数据
                    List<AgainData> notJoinInvitationAgain = groupPullMsgData.getNotJoinInvitationAgain();
                    if (notJoinInvitationAgain != null && notJoinInvitationAgain.size() > 0) {//插入数据库
                        for (AgainData againData : notJoinInvitationAgain) {
                            //未进群，再次邀请间隔时间(单位：秒）
                            Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                            //未进群，再次邀请拉群数据列表
                            List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                            if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                //拉近旧群才有未进群再次拉群营销数据，因为拉新群直接就进群了
                                GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                groupPullMarketing.setId(IdWorker.getId());
                                groupPullMarketing.setMerId(merId);
                                groupPullMarketing.setType(type);
                                groupPullMarketing.setPlanId(planId);
                                groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                groupPullMarketing.setDeviceId(device.getDeviceId());
                                groupPullMarketing.setWxid(wxid);
                                groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                groupPullMarketing.setStartTime(DateUtil.getTime());
                                groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                groupPullMarketing.setStatus(1); //开启中，拉进旧群默认不开启
                                groupPullMarketing.setModel(0); //不进群营销
                                groupPullMarketing.setHander(0);
                                groupPullMarketing.setHanderTime(null);
                                groupPullMarketing.setCreateTime(DateUtil.getTime());
                                groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                groupPullMarketingMapper.insertSelective(groupPullMarketing);
                            }
                        }
                    }

                    //开启已进群再次营销功能
                    List<AgainData> joinedMarketingAgain = groupPullMsgData.getJoinedMarketingAgain();
                    if (joinedMarketingAgain != null && joinedMarketingAgain.size() > 0) {//插入数据库
                        for (AgainData againData : joinedMarketingAgain) {
                            //已进群，再次营销间隔时间(单位：秒）
                            Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                            //已进群，再次营销拉群数据列表
                            List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                            if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                groupPullMarketing.setId(IdWorker.getId());
                                groupPullMarketing.setMerId(merId);
                                groupPullMarketing.setType(type);
                                groupPullMarketing.setPlanId(planId);
                                groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                groupPullMarketing.setDeviceId(device.getDeviceId());
                                groupPullMarketing.setWxid(wxid);
                                groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                groupPullMarketing.setStartTime(DateUtil.getTime());
                                groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                groupPullMarketing.setStatus(0);  //关闭中，进群通过才开启
                                groupPullMarketing.setModel(1); //已进群营销
                                groupPullMarketing.setHander(0);
                                groupPullMarketing.setHanderTime(null);
                                groupPullMarketing.setCreateTime(DateUtil.getTime());
                                groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                groupPullMarketingMapper.insertSelective(groupPullMarketing);
                            }
                        }
                    }
                } catch (Exception e) {
                    log.error("插入入群营销数据失败，error={}", e.getMessage(), e);
                }
            } else if (msgType.intValue() == 102) { //创建新群
                //发送创建群指令
                MicaTriggerGroupAddMsg micaTriggerGroupAddMsg = new MicaTriggerGroupAddMsg();
                micaTriggerGroupAddMsg.setGroupName(groupPullMsgData.getGroupName());
                micaTriggerGroupAddMsg.setWxIds(groupPullMsgData.getWxIds());
                MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerGroupAddMsg, MicaTriggerType.TRIGGER_GROUP_ADD, Functions.fillTriggerMsg(device));
                micaTriggerDto.setDelay(delay - difTime < 0 ? 0 : delay - difTime);
                sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息

                try {
                    //开启已进群再次营销功能
                    List<AgainData> joinedMarketingAgain = groupPullMsgData.getJoinedMarketingAgain();
                    if (joinedMarketingAgain != null && joinedMarketingAgain.size() > 0) {//插入数据库
                        for (AgainData againData : joinedMarketingAgain) {
                            //已进群，再次营销间隔时间(单位：秒）
                            Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                            //已进群，再次营销拉群数据列表
                            List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                            if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                groupPullMarketing.setId(IdWorker.getId());
                                groupPullMarketing.setMerId(merId);
                                groupPullMarketing.setType(type);
                                groupPullMarketing.setPlanId(planId);
                                groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                groupPullMarketing.setDeviceId(device.getDeviceId());
                                groupPullMarketing.setWxid(wxid);
                                groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                groupPullMarketing.setStartTime(DateUtil.getTime());
                                groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                groupPullMarketing.setStatus(1); //开启中，创建新群表示直接拉进去了
                                groupPullMarketing.setModel(1);  //已进群营销
                                groupPullMarketing.setHander(0);
                                groupPullMarketing.setHanderTime(null);
                                groupPullMarketing.setCreateTime(DateUtil.getTime());
                                groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                groupPullMarketingMapper.insertSelective(groupPullMarketing);
                            }
                        }
                    }
                } catch (Exception e) {
                    log.error("插入拉群营销数据失败，error={}", e.getMessage(), e);
                }
            }
        }
    }

    /**
     * 发送拉群消息接口
     *
     * @param device 机器人信息
     * @return 实例对象
     */
    @Async("taskExecutorForMsg")
    public void groupPullMsgSendByFaqReplay(@MerId Long merId, Device device, String wxid, String nickName, List<GroupPullMsgData> groupPullMsgDataList, Integer type, Long planId, boolean atAll, List<String> atWxIds, int msgsource, Boolean group) {
        if (device == null) {
            return;
        }

        if (StringUtils.isBlank(wxid)) {
            return;
        }

        if (groupPullMsgDataList == null || groupPullMsgDataList.size() == 0) {
            return;
        }

        int delay = 0; //默认延迟时间为0;
        for (int i = 0; i < groupPullMsgDataList.size(); i++) {
            GroupPullMsgData groupPullMsgData = groupPullMsgDataList.get(i);
            delay = delay + Tools.getInteger(groupPullMsgData.getGap()) * Tools.getInteger(groupPullMsgData.getUnit());
            Integer msgType = Tools.getInteger(groupPullMsgData.getMsgType());
            if (msgType.intValue() != 101 && msgType.intValue() != 102) {//普通消息
                MicaTriggerConversationTalkMsg micaTriggerConversationTalkMsg = new MicaTriggerConversationTalkMsg();
                micaTriggerConversationTalkMsg.setContentType(groupPullMsgData.getMsgType().intValue()); //类型
                micaTriggerConversationTalkMsg.setWxId(wxid); //发送方
                micaTriggerConversationTalkMsg.setGroup(group);
                micaTriggerConversationTalkMsg.setSource(msgsource);
                //处理文本消息替换变量
                if (groupPullMsgData.getMsgType().intValue() == MicaTalkMsgType.TEXT.getCode().intValue()) {//文本消息
                    String content = Tools.getStr(groupPullMsgData.getContent());

                    content = content.replace("[称呼/昵称]", Tools.getStr(nickName));
                    groupPullMsgData.setContent(content);
                }

                //转换成移动端接收的content格式
                String talkMsgContent = MsgDataTools.getTalkMsgContent(groupPullMsgData);

                //赋值消息内容
                micaTriggerConversationTalkMsg.setContent(talkMsgContent);
                micaTriggerConversationTalkMsg.setAtAll(atAll);
                micaTriggerConversationTalkMsg.setAtWxIds(atWxIds);
                MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerConversationTalkMsg, MicaTriggerType.TRIGGER_CONVERSATION_TALK, Functions.fillTriggerMsg(device));
                micaTriggerDto.setDelay(delay);
                sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息
            } else {
                if (msgType.intValue() == 101) { //拉进旧群
                    //发送拉群指令
                    MicaTriggerGroupInviteMsg micaTriggerGroupInviteMsg = new MicaTriggerGroupInviteMsg();
                    micaTriggerGroupInviteMsg.setGroupId(groupPullMsgData.getGroupId());
                    micaTriggerGroupInviteMsg.setWxIds(groupPullMsgData.getWxIds());
                    MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerGroupInviteMsg, MicaTriggerType.TRIGGER_GROUP_INVITE, Functions.fillTriggerMsg(device));
                    micaTriggerDto.setDelay(delay);
                    sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息

                    try {
                        //未进群营销数据
                        List<AgainData> notJoinInvitationAgain = groupPullMsgData.getNotJoinInvitationAgain();
                        if (notJoinInvitationAgain != null && notJoinInvitationAgain.size() > 0) {//插入数据库
                            for (AgainData againData : notJoinInvitationAgain) {
                                //未进群，再次邀请间隔时间(单位：秒）
                                Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                                //未进群，再次邀请拉群数据列表
                                List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                                if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                    //拉近旧群才有未进群再次拉群营销数据，因为拉新群直接就进群了
                                    GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                    groupPullMarketing.setId(IdWorker.getId());
                                    groupPullMarketing.setMerId(merId);
                                    groupPullMarketing.setType(type);
                                    groupPullMarketing.setPlanId(planId);
                                    groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                    groupPullMarketing.setDeviceId(device.getDeviceId());
                                    groupPullMarketing.setWxid(wxid);
                                    groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                    groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                    groupPullMarketing.setStartTime(DateUtil.getTime());
                                    groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                    groupPullMarketing.setStatus(1); //开启中，拉进旧群默认不开启
                                    groupPullMarketing.setModel(0); //不进群营销
                                    groupPullMarketing.setHander(0);
                                    groupPullMarketing.setHanderTime(null);
                                    groupPullMarketing.setCreateTime(DateUtil.getTime());
                                    groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                    groupPullMarketingMapper.insertSelective(groupPullMarketing);
                                }
                            }
                        }

                        //开启已进群再次营销功能
                        List<AgainData> joinedMarketingAgain = groupPullMsgData.getJoinedMarketingAgain();
                        if (joinedMarketingAgain != null && joinedMarketingAgain.size() > 0) {//插入数据库
                            for (AgainData againData : joinedMarketingAgain) {
                                //已进群，再次营销间隔时间(单位：秒）
                                Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                                //已进群，再次营销拉群数据列表
                                List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                                if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                    GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                    groupPullMarketing.setId(IdWorker.getId());
                                    groupPullMarketing.setMerId(merId);
                                    groupPullMarketing.setType(type);
                                    groupPullMarketing.setPlanId(planId);
                                    groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                    groupPullMarketing.setDeviceId(device.getDeviceId());
                                    groupPullMarketing.setWxid(wxid);
                                    groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                    groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                    groupPullMarketing.setStartTime(DateUtil.getTime());
                                    groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                    groupPullMarketing.setStatus(0);  //关闭中，进群通过才开启
                                    groupPullMarketing.setModel(1); //已进群营销
                                    groupPullMarketing.setHander(0);
                                    groupPullMarketing.setHanderTime(null);
                                    groupPullMarketing.setCreateTime(DateUtil.getTime());
                                    groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                    groupPullMarketingMapper.insertSelective(groupPullMarketing);
                                }
                            }
                        }
                    } catch (Exception e) {
                        log.error("插入入群营销数据失败", e.getMessage(), e);
                    }
                } else if (msgType.intValue() == 102) { //创建新群
                    //发送创建群指令
                    MicaTriggerGroupAddMsg micaTriggerGroupAddMsg = new MicaTriggerGroupAddMsg();
                    micaTriggerGroupAddMsg.setGroupName(groupPullMsgData.getGroupName());
                    micaTriggerGroupAddMsg.setWxIds(groupPullMsgData.getWxIds());
                    MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerGroupAddMsg, MicaTriggerType.TRIGGER_GROUP_ADD, Functions.fillTriggerMsg(device));
                    micaTriggerDto.setDelay(delay);
                    sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopicHolder.getNextServer()); //发送延迟消息

                    try {
                        //开启已进群再次营销功能
                        List<AgainData> joinedMarketingAgain = groupPullMsgData.getJoinedMarketingAgain();
                        if (joinedMarketingAgain != null && joinedMarketingAgain.size() > 0) {//插入数据库
                            for (AgainData againData : joinedMarketingAgain) {
                                //已进群，再次营销间隔时间(单位：秒）
                                Integer againIntervalTime = Tools.getInteger(againData.getAgainIntervalTime());
                                //已进群，再次营销拉群数据列表
                                List<GroupPullData> againGroupPullDatas = againData.getAgainGroupPullDatas();
                                if (againGroupPullDatas != null && againGroupPullDatas.size() > 0) {
                                    GroupPullMarketing groupPullMarketing = new GroupPullMarketing();
                                    groupPullMarketing.setId(IdWorker.getId());
                                    groupPullMarketing.setMerId(merId);
                                    groupPullMarketing.setType(type);
                                    groupPullMarketing.setPlanId(planId);
                                    groupPullMarketing.setDeviceUniqueId(device.getDeviceUniqueId());
                                    groupPullMarketing.setDeviceId(device.getDeviceId());
                                    groupPullMarketing.setWxid(wxid);
                                    groupPullMarketing.setGroupWxid(groupPullMsgData.getGroupId());
                                    groupPullMarketing.setGroupPullDatas(JSONObject.toJSONString(againGroupPullDatas));
                                    groupPullMarketing.setStartTime(DateUtil.getTime());
                                    groupPullMarketing.setDelay(againIntervalTime + delay); //页面设置的延迟时间+延迟发送拉群时间
                                    groupPullMarketing.setStatus(1); //开启中，创建新群表示直接拉进去了
                                    groupPullMarketing.setModel(1);  //已进群营销
                                    groupPullMarketing.setHander(0);
                                    groupPullMarketing.setHanderTime(null);
                                    groupPullMarketing.setCreateTime(DateUtil.getTime());
                                    groupPullMarketing.setUpdateTime(DateUtil.getTime());
                                    groupPullMarketingMapper.insertSelective(groupPullMarketing);
                                }
                            }
                        }
                    } catch (Exception e) {
                        log.error("插入拉群营销数据失败，error={}", e.getMessage(), e);
                    }
                }
            }
        }
    }


}